
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge" />
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Admin actions &#8212; Django 2.2.12.dev20200304094918 documentation</title>
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <script type="text/javascript" src="../../../_static/language_data.js"></script>
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" />
    <link rel="next" title="The Django admin documentation generator" href="admindocs.html" />
    <link rel="prev" title="The Django admin site" href="index.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);</script>

  </head><body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 2.2.12.dev20200304094918 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    &laquo; <a href="index.html" title="The Django admin site">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="admindocs.html" title="The Django admin documentation generator">next</a> &raquo;</div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="ref-contrib-admin-actions">
            
  <div class="section" id="s-admin-actions">
<span id="admin-actions"></span><h1>Admin actions<a class="headerlink" href="#admin-actions" title="Permalink to this headline">¶</a></h1>
<p>The basic workflow of Django’s admin is, in a nutshell, “select an object,
then change it.” This works well for a majority of use cases. However, if you
need to make the same change to many objects at once, this workflow can be
quite tedious.</p>
<p>In these cases, Django’s admin lets you write and register “actions” – simple
functions that get called with a list of objects selected on the change list
page.</p>
<p>If you look at any change list in the admin, you’ll see this feature in
action; Django ships with a “delete selected objects” action available to all
models. For example, here’s the user module from Django’s built-in
<a class="reference internal" href="../../../topics/auth/index.html#module-django.contrib.auth" title="django.contrib.auth: Django's authentication framework."><code class="xref py py-mod docutils literal notranslate"><span class="pre">django.contrib.auth</span></code></a> app:</p>
<img alt="../../../_images/admin-actions.png" src="../../../_images/admin-actions.png" />
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p>The “delete selected objects” action uses <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet.delete" title="django.db.models.query.QuerySet.delete"><code class="xref py py-meth docutils literal notranslate"><span class="pre">QuerySet.delete()</span></code></a> for efficiency reasons, which
has an important caveat: your model’s <code class="docutils literal notranslate"><span class="pre">delete()</span></code> method will not be
called.</p>
<p>If you wish to override this behavior, you can override
<a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.delete_queryset" title="django.contrib.admin.ModelAdmin.delete_queryset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.delete_queryset()</span></code></a> or write a custom action which does
deletion in your preferred manner – for example, by calling
<code class="docutils literal notranslate"><span class="pre">Model.delete()</span></code> for each of the selected items.</p>
<p class="last">For more background on bulk deletion, see the documentation on <a class="reference internal" href="../../../topics/db/queries.html#topics-db-queries-delete"><span class="std std-ref">object
deletion</span></a>.</p>
</div>
<p>Read on to find out how to add your own actions to this list.</p>
<div class="section" id="s-writing-actions">
<span id="writing-actions"></span><h2>Writing actions<a class="headerlink" href="#writing-actions" title="Permalink to this headline">¶</a></h2>
<p>The easiest way to explain actions is by example, so let’s dive in.</p>
<p>A common use case for admin actions is the bulk updating of a model. Imagine a
simple news application with an <code class="docutils literal notranslate"><span class="pre">Article</span></code> model:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.db</span> <span class="k">import</span> <span class="n">models</span>

<span class="n">STATUS_CHOICES</span> <span class="o">=</span> <span class="p">[</span>
    <span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">,</span> <span class="s1">&#39;Draft&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;p&#39;</span><span class="p">,</span> <span class="s1">&#39;Published&#39;</span><span class="p">),</span>
    <span class="p">(</span><span class="s1">&#39;w&#39;</span><span class="p">,</span> <span class="s1">&#39;Withdrawn&#39;</span><span class="p">),</span>
<span class="p">]</span>

<span class="k">class</span> <span class="nc">Article</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="n">title</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
    <span class="n">body</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">TextField</span><span class="p">()</span>
    <span class="n">status</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">CharField</span><span class="p">(</span><span class="n">max_length</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">choices</span><span class="o">=</span><span class="n">STATUS_CHOICES</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">title</span>
</pre></div>
</div>
<p>A common task we might perform with a model like this is to update an
article’s status from “draft” to “published”. We could easily do this in the
admin one article at a time, but if we wanted to bulk-publish a group of
articles, it’d be tedious. So, let’s write an action that lets us change an
article’s status to “published.”</p>
<div class="section" id="s-writing-action-functions">
<span id="writing-action-functions"></span><h3>Writing action functions<a class="headerlink" href="#writing-action-functions" title="Permalink to this headline">¶</a></h3>
<p>First, we’ll need to write a function that gets called when the action is
triggered from the admin. Action functions are just regular functions that take
three arguments:</p>
<ul class="simple">
<li>The current <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a></li>
<li>An <a class="reference internal" href="../../request-response.html#django.http.HttpRequest" title="django.http.HttpRequest"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpRequest</span></code></a> representing the current request,</li>
<li>A <a class="reference internal" href="../../models/querysets.html#django.db.models.query.QuerySet" title="django.db.models.query.QuerySet"><code class="xref py py-class docutils literal notranslate"><span class="pre">QuerySet</span></code></a> containing the set of
objects selected by the user.</li>
</ul>
<p>Our publish-these-articles function won’t need the <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> or the
request object, but we will use the queryset:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>For the best performance, we’re using the queryset’s <a class="reference internal" href="../../../topics/db/queries.html#topics-db-queries-update"><span class="std std-ref">update method</span></a>. Other types of actions might need to deal
with each object individually; in these cases we’d just iterate over the
queryset:</p>
<div class="last highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">for</span> <span class="n">obj</span> <span class="ow">in</span> <span class="n">queryset</span><span class="p">:</span>
    <span class="n">do_something_with</span><span class="p">(</span><span class="n">obj</span><span class="p">)</span>
</pre></div>
</div>
</div>
<p>That’s actually all there is to writing an action! However, we’ll take one
more optional-but-useful step and give the action a “nice” title in the admin.
By default, this action would appear in the action list as “Make published” –
the function name, with underscores replaced by spaces. That’s fine, but we
can provide a better, more human-friendly name by giving the
<code class="docutils literal notranslate"><span class="pre">make_published</span></code> function a <code class="docutils literal notranslate"><span class="pre">short_description</span></code> attribute:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
<span class="n">make_published</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Mark selected stories as published&quot;</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This might look familiar; the admin’s <code class="docutils literal notranslate"><span class="pre">list_display</span></code> option uses the
same technique to provide human-readable descriptions for callback
functions registered there, too.</p>
</div>
</div>
<div class="section" id="s-adding-actions-to-the-modeladmin">
<span id="adding-actions-to-the-modeladmin"></span><h3>Adding actions to the <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a><a class="headerlink" href="#adding-actions-to-the-modeladmin" title="Permalink to this headline">¶</a></h3>
<p>Next, we’ll need to inform our <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> of the action. This works
just like any other configuration option. So, the complete <code class="docutils literal notranslate"><span class="pre">admin.py</span></code> with
the action and its registration would look like:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">myapp.models</span> <span class="k">import</span> <span class="n">Article</span>

<span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
<span class="n">make_published</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Mark selected stories as published&quot;</span>

<span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">list_display</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">,</span> <span class="s1">&#39;status&#39;</span><span class="p">]</span>
    <span class="n">ordering</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;title&#39;</span><span class="p">]</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="p">[</span><span class="n">make_published</span><span class="p">]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Article</span><span class="p">,</span> <span class="n">ArticleAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>That code will give us an admin change list that looks something like this:</p>
<img alt="../../../_images/adding-actions-to-the-modeladmin.png" src="../../../_images/adding-actions-to-the-modeladmin.png" />
<p>That’s really all there is to it! If you’re itching to write your own actions,
you now know enough to get started. The rest of this document just covers more
advanced techniques.</p>
</div>
<div class="section" id="s-handling-errors-in-actions">
<span id="handling-errors-in-actions"></span><h3>Handling errors in actions<a class="headerlink" href="#handling-errors-in-actions" title="Permalink to this headline">¶</a></h3>
<p>If there are foreseeable error conditions that may occur while running your
action, you should gracefully inform the user of the problem. This means
handling exceptions and using
<a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.message_user" title="django.contrib.admin.ModelAdmin.message_user"><code class="xref py py-meth docutils literal notranslate"><span class="pre">django.contrib.admin.ModelAdmin.message_user()</span></code></a> to display a user friendly
description of the problem in the response.</p>
</div>
</div>
<div class="section" id="s-advanced-action-techniques">
<span id="advanced-action-techniques"></span><h2>Advanced action techniques<a class="headerlink" href="#advanced-action-techniques" title="Permalink to this headline">¶</a></h2>
<p>There’s a couple of extra options and possibilities you can exploit for more
advanced options.</p>
<div class="section" id="s-actions-as-modeladmin-methods">
<span id="actions-as-modeladmin-methods"></span><h3>Actions as <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> methods<a class="headerlink" href="#actions-as-modeladmin-methods" title="Permalink to this headline">¶</a></h3>
<p>The example above shows the <code class="docutils literal notranslate"><span class="pre">make_published</span></code> action defined as a simple
function. That’s perfectly fine, but it’s not perfect from a code design point
of view: since the action is tightly coupled to the <code class="docutils literal notranslate"><span class="pre">Article</span></code> object, it
makes sense to hook the action to the <code class="docutils literal notranslate"><span class="pre">ArticleAdmin</span></code> object itself.</p>
<p>That’s easy enough to do:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="o">...</span>

    <span class="n">actions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;make_published&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
    <span class="n">make_published</span><span class="o">.</span><span class="n">short_description</span> <span class="o">=</span> <span class="s2">&quot;Mark selected stories as published&quot;</span>
</pre></div>
</div>
<p>Notice first that we’ve moved <code class="docutils literal notranslate"><span class="pre">make_published</span></code> into a method and renamed the
<code class="docutils literal notranslate"><span class="pre">modeladmin</span></code> parameter to <code class="docutils literal notranslate"><span class="pre">self</span></code>, and second that we’ve now put the string
<code class="docutils literal notranslate"><span class="pre">'make_published'</span></code> in <code class="docutils literal notranslate"><span class="pre">actions</span></code> instead of a direct function reference. This
tells the <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> to look up the action as a method.</p>
<p>Defining actions as methods gives the action more straightforward, idiomatic
access to the <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> itself, allowing the action to call any of the
methods provided by the admin.</p>
<p id="custom-admin-action">For example, we can use <code class="docutils literal notranslate"><span class="pre">self</span></code> to flash a message to the user informing her
that the action was successful:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="n">rows_updated</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">rows_updated</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">message_bit</span> <span class="o">=</span> <span class="s2">&quot;1 story was&quot;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">message_bit</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> stories were&quot;</span> <span class="o">%</span> <span class="n">rows_updated</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">message_user</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="s2">&quot;</span><span class="si">%s</span><span class="s2"> successfully marked as published.&quot;</span> <span class="o">%</span> <span class="n">message_bit</span><span class="p">)</span>
</pre></div>
</div>
<p>This make the action match what the admin itself does after successfully
performing an action:</p>
<img alt="../../../_images/actions-as-modeladmin-methods.png" src="../../../_images/actions-as-modeladmin-methods.png" />
</div>
<div class="section" id="s-actions-that-provide-intermediate-pages">
<span id="actions-that-provide-intermediate-pages"></span><h3>Actions that provide intermediate pages<a class="headerlink" href="#actions-that-provide-intermediate-pages" title="Permalink to this headline">¶</a></h3>
<p>By default, after an action is performed the user is simply redirected back
to the original change list page. However, some actions, especially more
complex ones, will need to return intermediate pages. For example, the
built-in delete action asks for confirmation before deleting the selected
objects.</p>
<p>To provide an intermediary page, simply return an
<a class="reference internal" href="../../request-response.html#django.http.HttpResponse" title="django.http.HttpResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponse</span></code></a> (or subclass) from your action. For
example, you might write a simple export function that uses Django’s
<a class="reference internal" href="../../../topics/serialization.html"><span class="doc">serialization functions</span></a> to dump some selected
objects as JSON:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.core</span> <span class="k">import</span> <span class="n">serializers</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponse</span>

<span class="k">def</span> <span class="nf">export_as_json</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">response</span> <span class="o">=</span> <span class="n">HttpResponse</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="s2">&quot;application/json&quot;</span><span class="p">)</span>
    <span class="n">serializers</span><span class="o">.</span><span class="n">serialize</span><span class="p">(</span><span class="s2">&quot;json&quot;</span><span class="p">,</span> <span class="n">queryset</span><span class="p">,</span> <span class="n">stream</span><span class="o">=</span><span class="n">response</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">response</span>
</pre></div>
</div>
<p>Generally, something like the above isn’t considered a great idea. Most of the
time, the best practice will be to return an
<a class="reference internal" href="../../request-response.html#django.http.HttpResponseRedirect" title="django.http.HttpResponseRedirect"><code class="xref py py-class docutils literal notranslate"><span class="pre">HttpResponseRedirect</span></code></a> and redirect the user to a view
you’ve written, passing the list of selected objects in the GET query string.
This allows you to provide complex interaction logic on the intermediary
pages. For example, if you wanted to provide a more complete export function,
you’d want to let the user choose a format, and possibly a list of fields to
include in the export. The best thing to do would be to write a small action
that simply redirects to your custom export view:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib.contenttypes.models</span> <span class="k">import</span> <span class="n">ContentType</span>
<span class="kn">from</span> <span class="nn">django.http</span> <span class="k">import</span> <span class="n">HttpResponseRedirect</span>

<span class="k">def</span> <span class="nf">export_selected_objects</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">selected</span> <span class="o">=</span> <span class="n">queryset</span><span class="o">.</span><span class="n">values_list</span><span class="p">(</span><span class="s1">&#39;pk&#39;</span><span class="p">,</span> <span class="n">flat</span><span class="o">=</span><span class="kc">True</span><span class="p">)</span>
    <span class="n">ct</span> <span class="o">=</span> <span class="n">ContentType</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">get_for_model</span><span class="p">(</span><span class="n">queryset</span><span class="o">.</span><span class="n">model</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">HttpResponseRedirect</span><span class="p">(</span><span class="s1">&#39;/export/?ct=</span><span class="si">%s</span><span class="s1">&amp;ids=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span>
        <span class="n">ct</span><span class="o">.</span><span class="n">pk</span><span class="p">,</span>
        <span class="s1">&#39;,&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">pk</span><span class="p">)</span> <span class="k">for</span> <span class="n">pk</span> <span class="ow">in</span> <span class="n">selected</span><span class="p">),</span>
    <span class="p">))</span>
</pre></div>
</div>
<p>As you can see, the action is the simple part; all the complex logic would
belong in your export view. This would need to deal with objects of any type,
hence the business with the <code class="docutils literal notranslate"><span class="pre">ContentType</span></code>.</p>
<p>Writing this view is left as an exercise to the reader.</p>
</div>
<div class="section" id="s-making-actions-available-site-wide">
<span id="s-adminsite-actions"></span><span id="making-actions-available-site-wide"></span><span id="adminsite-actions"></span><h3>Making actions available site-wide<a class="headerlink" href="#making-actions-available-site-wide" title="Permalink to this headline">¶</a></h3>
<dl class="method">
<dt id="django.contrib.admin.AdminSite.add_action">
<code class="descclassname">AdminSite.</code><code class="descname">add_action</code>(<em>action</em>, <em>name=None</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite.add_action"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite.add_action" title="Permalink to this definition">¶</a></dt>
<dd><p>Some actions are best if they’re made available to <em>any</em> object in the admin
site – the export action defined above would be a good candidate. You can
make an action globally available using <a class="reference internal" href="#django.contrib.admin.AdminSite.add_action" title="django.contrib.admin.AdminSite.add_action"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AdminSite.add_action()</span></code></a>. For
example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">add_action</span><span class="p">(</span><span class="n">export_selected_objects</span><span class="p">)</span>
</pre></div>
</div>
<p>This makes the <code class="docutils literal notranslate"><span class="pre">export_selected_objects</span></code> action globally available as an
action named “export_selected_objects”. You can explicitly give the action
a name – good if you later want to programmatically <a class="reference internal" href="#disabling-admin-actions"><span class="std std-ref">remove the action</span></a> – by passing a second argument to
<a class="reference internal" href="#django.contrib.admin.AdminSite.add_action" title="django.contrib.admin.AdminSite.add_action"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AdminSite.add_action()</span></code></a>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">add_action</span><span class="p">(</span><span class="n">export_selected_objects</span><span class="p">,</span> <span class="s1">&#39;export_selected&#39;</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-disabling-actions">
<span id="s-disabling-admin-actions"></span><span id="disabling-actions"></span><span id="disabling-admin-actions"></span><h3>Disabling actions<a class="headerlink" href="#disabling-actions" title="Permalink to this headline">¶</a></h3>
<p>Sometimes you need to disable certain actions – especially those
<a class="reference internal" href="#adminsite-actions"><span class="std std-ref">registered site-wide</span></a> – for particular objects.
There’s a few ways you can disable actions:</p>
<div class="section" id="s-disabling-a-site-wide-action">
<span id="disabling-a-site-wide-action"></span><h4>Disabling a site-wide action<a class="headerlink" href="#disabling-a-site-wide-action" title="Permalink to this headline">¶</a></h4>
<dl class="method">
<dt id="django.contrib.admin.AdminSite.disable_action">
<code class="descclassname">AdminSite.</code><code class="descname">disable_action</code>(<em>name</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/sites.html#AdminSite.disable_action"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.AdminSite.disable_action" title="Permalink to this definition">¶</a></dt>
<dd><p>If you need to disable a <a class="reference internal" href="#adminsite-actions"><span class="std std-ref">site-wide action</span></a> you can
call <a class="reference internal" href="#django.contrib.admin.AdminSite.disable_action" title="django.contrib.admin.AdminSite.disable_action"><code class="xref py py-meth docutils literal notranslate"><span class="pre">AdminSite.disable_action()</span></code></a>.</p>
<p>For example, you can use this method to remove the built-in “delete selected
objects” action:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">disable_action</span><span class="p">(</span><span class="s1">&#39;delete_selected&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Once you’ve done the above, that action will no longer be available
site-wide.</p>
<p>If, however, you need to re-enable a globally-disabled action for one
particular model, simply list it explicitly in your <code class="docutils literal notranslate"><span class="pre">ModelAdmin.actions</span></code>
list:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Globally disable delete selected</span>
<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">disable_action</span><span class="p">(</span><span class="s1">&#39;delete_selected&#39;</span><span class="p">)</span>

<span class="c1"># This ModelAdmin will not have delete_selected available</span>
<span class="k">class</span> <span class="nc">SomeModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;some_other_action&#39;</span><span class="p">]</span>
    <span class="o">...</span>

<span class="c1"># This one will</span>
<span class="k">class</span> <span class="nc">AnotherModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;delete_selected&#39;</span><span class="p">,</span> <span class="s1">&#39;a_third_action&#39;</span><span class="p">]</span>
    <span class="o">...</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="s-disabling-all-actions-for-a-particular-modeladmin">
<span id="disabling-all-actions-for-a-particular-modeladmin"></span><h4>Disabling all actions for a particular <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a><a class="headerlink" href="#disabling-all-actions-for-a-particular-modeladmin" title="Permalink to this headline">¶</a></h4>
<p>If you want <em>no</em> bulk actions available for a given <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a>, simply
set <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.actions" title="django.contrib.admin.ModelAdmin.actions"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ModelAdmin.actions</span></code></a> to <code class="docutils literal notranslate"><span class="pre">None</span></code>:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="kc">None</span>
</pre></div>
</div>
<p>This tells the <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin" title="django.contrib.admin.ModelAdmin"><code class="xref py py-class docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a> to not display or allow any actions,
including any <a class="reference internal" href="#adminsite-actions"><span class="std std-ref">site-wide actions</span></a>.</p>
</div>
<div class="section" id="s-conditionally-enabling-or-disabling-actions">
<span id="conditionally-enabling-or-disabling-actions"></span><h4>Conditionally enabling or disabling actions<a class="headerlink" href="#conditionally-enabling-or-disabling-actions" title="Permalink to this headline">¶</a></h4>
<dl class="method">
<dt id="django.contrib.admin.ModelAdmin.get_actions">
<code class="descclassname">ModelAdmin.</code><code class="descname">get_actions</code>(<em>request</em>)<a class="reference internal" href="../../../_modules/django/contrib/admin/options.html#ModelAdmin.get_actions"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#django.contrib.admin.ModelAdmin.get_actions" title="Permalink to this definition">¶</a></dt>
<dd><p>Finally, you can conditionally enable or disable actions on a per-request
(and hence per-user basis) by overriding <a class="reference internal" href="#django.contrib.admin.ModelAdmin.get_actions" title="django.contrib.admin.ModelAdmin.get_actions"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.get_actions()</span></code></a>.</p>
<p>This returns a dictionary of actions allowed. The keys are action names, and
the values are <code class="docutils literal notranslate"><span class="pre">(function,</span> <span class="pre">name,</span> <span class="pre">short_description)</span></code> tuples.</p>
<p>For example, if you only want users whose names begin with ‘J’ to be able
to delete objects in bulk:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="o">...</span>

    <span class="k">def</span> <span class="nf">get_actions</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="n">actions</span> <span class="o">=</span> <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="n">get_actions</span><span class="p">(</span><span class="n">request</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">username</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span> <span class="o">!=</span> <span class="s1">&#39;J&#39;</span><span class="p">:</span>
            <span class="k">if</span> <span class="s1">&#39;delete_selected&#39;</span> <span class="ow">in</span> <span class="n">actions</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">actions</span><span class="p">[</span><span class="s1">&#39;delete_selected&#39;</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">actions</span>
</pre></div>
</div>
</dd></dl>

</div>
</div>
<div class="section" id="s-setting-permissions-for-actions">
<span id="s-admin-action-permissions"></span><span id="setting-permissions-for-actions"></span><span id="admin-action-permissions"></span><h3>Setting permissions for actions<a class="headerlink" href="#setting-permissions-for-actions" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<span class="title">New in Django 2.1.</span> </div>
<p>Actions may limit their availability to users with specific permissions by
setting an <code class="docutils literal notranslate"><span class="pre">allowed_permissions</span></code> attribute on the action function:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="n">modeladmin</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
    <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
<span class="n">make_published</span><span class="o">.</span><span class="n">allowed_permissions</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;change&#39;</span><span class="p">,)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">make_published()</span></code> action will only be available to users that pass the
<a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.has_change_permission" title="django.contrib.admin.ModelAdmin.has_change_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.has_change_permission()</span></code></a> check.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">allowed_permissions</span></code> has more than one permission, the action will be
available as long as the user passes at least one of the checks.</p>
<p>Available values for <code class="docutils literal notranslate"><span class="pre">allowed_permissions</span></code> and the corresponding method
checks are:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'add'</span></code>: <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.has_add_permission" title="django.contrib.admin.ModelAdmin.has_add_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.has_add_permission()</span></code></a></li>
<li><code class="docutils literal notranslate"><span class="pre">'change'</span></code>: <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.has_change_permission" title="django.contrib.admin.ModelAdmin.has_change_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.has_change_permission()</span></code></a></li>
<li><code class="docutils literal notranslate"><span class="pre">'delete'</span></code>: <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.has_delete_permission" title="django.contrib.admin.ModelAdmin.has_delete_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.has_delete_permission()</span></code></a></li>
<li><code class="docutils literal notranslate"><span class="pre">'view'</span></code>: <a class="reference internal" href="index.html#django.contrib.admin.ModelAdmin.has_view_permission" title="django.contrib.admin.ModelAdmin.has_view_permission"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ModelAdmin.has_view_permission()</span></code></a></li>
</ul>
<p>You can specify any other value as long as you implement a corresponding
<code class="docutils literal notranslate"><span class="pre">has_&lt;value&gt;_permission(self,</span> <span class="pre">request)</span></code> method on the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code>.</p>
<p>For example:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">django.contrib</span> <span class="k">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">django.contrib.auth</span> <span class="k">import</span> <span class="n">get_permission_codename</span>

<span class="k">class</span> <span class="nc">ArticleAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">actions</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;make_published&#39;</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">make_published</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="n">queryset</span><span class="p">):</span>
        <span class="n">queryset</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">status</span><span class="o">=</span><span class="s1">&#39;p&#39;</span><span class="p">)</span>
    <span class="n">make_published</span><span class="o">.</span><span class="n">allowed_permissions</span> <span class="o">=</span> <span class="p">(</span><span class="s1">&#39;publish&#39;</span><span class="p">,)</span>

    <span class="k">def</span> <span class="nf">has_publish_permission</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">request</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Does the user have the publish permission?&quot;&quot;&quot;</span>
        <span class="n">opts</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">opts</span>
        <span class="n">codename</span> <span class="o">=</span> <span class="n">get_permission_codename</span><span class="p">(</span><span class="s1">&#39;publish&#39;</span><span class="p">,</span> <span class="n">opts</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">request</span><span class="o">.</span><span class="n">user</span><span class="o">.</span><span class="n">has_perm</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">opts</span><span class="o">.</span><span class="n">app_label</span><span class="p">,</span> <span class="n">codename</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../../../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">Admin actions</a><ul>
<li><a class="reference internal" href="#writing-actions">Writing actions</a><ul>
<li><a class="reference internal" href="#writing-action-functions">Writing action functions</a></li>
<li><a class="reference internal" href="#adding-actions-to-the-modeladmin">Adding actions to the <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a></li>
<li><a class="reference internal" href="#handling-errors-in-actions">Handling errors in actions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#advanced-action-techniques">Advanced action techniques</a><ul>
<li><a class="reference internal" href="#actions-as-modeladmin-methods">Actions as <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code> methods</a></li>
<li><a class="reference internal" href="#actions-that-provide-intermediate-pages">Actions that provide intermediate pages</a></li>
<li><a class="reference internal" href="#making-actions-available-site-wide">Making actions available site-wide</a></li>
<li><a class="reference internal" href="#disabling-actions">Disabling actions</a><ul>
<li><a class="reference internal" href="#disabling-a-site-wide-action">Disabling a site-wide action</a></li>
<li><a class="reference internal" href="#disabling-all-actions-for-a-particular-modeladmin">Disabling all actions for a particular <code class="docutils literal notranslate"><span class="pre">ModelAdmin</span></code></a></li>
<li><a class="reference internal" href="#conditionally-enabling-or-disabling-actions">Conditionally enabling or disabling actions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#setting-permissions-for-actions">Setting permissions for actions</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="index.html"
                        title="previous chapter">The Django admin site</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="admindocs.html"
                        title="next chapter">The Django admin documentation generator</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../../_sources/ref/contrib/admin/actions.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Mar 04, 2020</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    &laquo; <a href="index.html" title="The Django admin site">previous</a>
     |
    <a href="../../index.html" title="API Reference" accesskey="U">up</a>
   |
    <a href="admindocs.html" title="The Django admin documentation generator">next</a> &raquo;</div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>